
package net.sky.oa.service.system.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sky.oa.dao.system.SysEmployeeDao;
import net.sky.oa.dao.system.SysEmployeeLeaveDao;
import net.sky.oa.model.SysEmployee;
import net.sky.oa.model.SysEmployeeLeave;
import net.sky.oa.service.system.SysEmployeeLeaveService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htsoft.core.service.impl.BaseServiceImpl;
import com.htsoft.core.web.paging.PagingBean;

@Service
@Transactional
public class SysEmployeeLeaveServiceImpl extends BaseServiceImpl<SysEmployeeLeave> implements SysEmployeeLeaveService {
    
	
	private SysEmployeeLeaveDao sysEmployeeLeavedao;
	private SysEmployeeDao sysEmployeedao;
	
	@Resource
	public void setDao(SysEmployeeLeaveDao sysEmployeeLeavedao){
		super.setDao(sysEmployeeLeavedao);
	    this.sysEmployeeLeavedao=sysEmployeeLeavedao;	
	}
	
	@Resource
	public void setSysEmployeeDao(SysEmployeeDao sysEmployeedao) {
		this.sysEmployeedao = sysEmployeedao;
	}
    
	@Transactional(readOnly=true)
	public List<SysEmployeeLeave> findAllLeave() {
		
		return this.sysEmployeeLeavedao.findAllLeave();
	}

	public List<SysEmployeeLeave> findByHql(String paramString) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<SysEmployeeLeave> getLeave(Long userId, Integer type, PagingBean pb){
		return this.sysEmployeeLeavedao.getLeave(userId, type, pb);
	}
	
	@Override
	public SysEmployeeLeave save(SysEmployeeLeave entity) {
		
		SysEmployee employee = sysEmployeedao.get(entity.getUser().getId());
		if(null != employee){
			if(entity.getType().equals(3)){
				if(null == employee.getPeoplecount()){
					employee.setPeoplecount(1);
				}else{
					employee.setPeoplecount(employee.getPeoplecount()+1);
				}
			}else{
				if(null == employee.getCount()){
					employee.setCount(1);
				}else{
					employee.setCount(employee.getCount()+1);
				}
			}
			sysEmployeedao.save(employee);
		}
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = formatter.format(new Date());
		entity.setTime(time);
		return super.save(entity);
	}
	
}
